<?php require_once("common/header.php"); ?>
<script type="text/javascript" src="/style/js/sweet-alert.min.js"></script>
<link type="text/css" rel="stylesheet" href="/style/css/sweet-alert.css">
<script src="/style/js/dropload.js"></script>
<script src="/style/js/dropload.min.js"></script>
<link href="/style/css/dropload.css" rel="stylesheet">
<style>
    .outer {
        position: absolute;
        left: 0;
        width: 100%;
        height: 100%;
        display: -webkit-box;
        display: -webkit-flex;
        display: -ms-flexbox;
        display: flex;
        -ms-flex-direction: column;
        -webkit-box-orient: vertical;
        box-orient: vertical;
        -webkit-flex-direction: column;
        flex-direction: column;
    }

    .inner {
        -webkit-box-flex: 1;
        -webkit-flex: 1;
        -ms-flex: 1;
        flex: 1;
        background-color: #f1f1f1;
        overflow-y: scroll;
        -webkit-overflow-scrolling: touch;
    }

    .opacity {
        -webkit-animation: opacity 0.3s linear;
        animation: opacity 0.3s linear;
    }

    @-webkit-keyframes opacity {
        0% {
            opacity: 0;
        }
        100% {
            opacity: 1;
        }
    }

    @keyframes opacity {
        0% {
            opacity: 0;
        }
        100% {
            opacity: 1;
        }
    }
</style>
<body>
<div class="outwrap">
    <!--top-->
    <!-- <div class="topbar clearfix topvh">
        <a href="/myshop" class="btn-back">
        <a href="/user/index" class="btn-back">
            <i class="ico-back"></i><span>返回</span>
        </a>
    </div> -->
    <div class="meinner-adr">
        <div class="adr-box">
            <div class="outer">
                <div class="inner">
                    <ul class="adr-list" id="thelist">
                        <?php if ($this->data['data']) {
                            foreach ($this->data['data'] as $key => $item):?>
                                <li>
                                    <div class="title clearfix"><h4><?php echo $item['realname']; ?></h4>
                                        <span><?= substr_replace( $item['mobile'],'****',3,4)?></span></div>
                                    <p><?php if ($item['province_id'] > 0) echo $this->data['region'][$item['province_id']];
                                        if ($item['city_id'] > 0) echo $this->data['region'][$item['city_id']];
                                        if ($item['county_id'] > 0) echo $this->data['region'][$item['county_id']];
                                        echo ' ' . $item['address']; ?></p>
                                        <?php if(!empty($item['idnum'])){?>
                                         <p><?= substr_replace( $item['idnum'],'***********',3,11)?></p>
                                         <?php }?>
                                    <div class="line"></div>
                                    <div class="opr-bar">
                                        <?php if ($item['is_default']): ?>
                                            <span class="moren moren-on"><i class="ico"></i>默认地址</span>
                                        <?php else: ?>
                                            <span class="moren"
                                                  onClick="setUserAddress(<?php echo $item['id']; ?>,this)"><i
                                                        class="ico"></i>设为默认</span>
                                        <?php endif; ?>
                                        <a href="edit_user_address?id=<?= $item['id'] ?>"><i class="ico-bj"></i>编辑</a>
                                        <a href="javascript:;"
                                           onClick="delUserAddress(<?php echo $item['id']; ?>,this)"><i
                                                    class="ico-sc"></i>删除</a>
                                    </div>
                                </li>
                            <?php endforeach; ?>
                        <?php } ?>
                    </ul>
                </div>
            </div>
        </div>

        <!--暂无 地址-->
        <div class="screen-none">
            <div class="screen-none-pic"><img src="../../style/images/kong-7.png"/></div>
            <p class="screen-none-font">您还没有设置收货地址哦！</p>
        </div>
    </div>

    <!--底部导航-->
    <div class="btn-tjadr">
        <a href="#" onclick="address_number()">添加收货地址</a>
    </div>

</div>
<script type="text/javascript">

    //地址限定
    function address_number() {
        $.post("number_ajax",{act:'address_number'},function(data){
            if(data.code==900){
                swal("地址不能超过3个",false,"error");
            }else if(data.code==200){
                window.location.href = 'add_useraddr';
            }
        },'json');
    }

    function delUserAddress(dataid, obj) {
        swal({
                title: "确定删除此条地址？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确定",
                cancelButtonText: "取消"
            },
            function () {
                $.ajax({
                    type: "GET",
                    url: "/user/ajax_del_address",
                    data: {id: dataid},
                    dataType: "json",
                    success: function (data) {
                        if (data.success == 1) {
                            $(obj).parents("li").remove();
                        } else {
                            swal(data.msg);
                        }
                    }
                });
            });
    }

    function setUserAddress(dataid, obj) {
        swal({
                title: "确定设为默认地址？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确定",
                cancelButtonText: "取消"
            },
            function () {
                $.ajax({
                    type: "GET",
                    url: "/user/ajax_set_address",
                    data: {id: dataid},
                    dataType: "json",
                    success: function (data) {
                        if (data.success == 1) {
                            window.location.href = window.location.href;
                        } else {
                            swal(data.msg);
                        }
                    }
                });
            });
    }

</script>
<script>
    var p =<?=$this->data['page']?>, now_total =<?=$this->data['count']?>, is_over = false, is_top = false, pagesize = 10;
    console.log("p ready" + p);
    if (p == 1) {
        console.log("is_top:" + p + "   " + is_top);
        is_top = true;
        $('.dropload-up > .dropload-refresh').html("无更多数据");

    }
    $(document).ready(function () {
        // dropload
        var dropload = $('.inner').dropload({
            domUp: {
                domClass: 'dropload-up',
                domRefresh: '<div class="dropload-refresh">↓下拉刷新</div>',
                domUpdate: '<div class="dropload-update">↑释放更新</div>',
                domLoad: '<div class="dropload-load"><span class="loading"></span>加载中...</div>'
            },
            domDown: {
                domClass: 'dropload-down',
                domRefresh: '<div class="dropload-refresh">↑上拉加载更多</div>',
                domLoad: '<div class="dropload-load"><span class="loading"></span>加载中...</div>',
                domNoData: '<div class="dropload-noData">已无更多商品</div>'
            },
            loadDownFn: function (me) {
                if (!is_over) {
                    console.log("加载数据开始..");
                    p = parseInt(p) + 1;
                    console.log(p);
                    $.ajax({
                        type: "get",
                        url: "addr_ajax",
                        data: {page: p},
                        async: false,
                        dataType: 'json',
                        success: function (data) {
                            shtml = "";
                            ii = 0;
                            $(data.data).each(function (i, v) {
                                ii++;
                                shtml += '<li><div class="title clearfix"><h4>' + v.realname + '</h4><span>' + v.mobile + '</span></div><p>';
                                if (v.province_id > 0)
                                    shtml += data.region[v.province_id];
                                if (v.city_id > 0)
                                    shtml += data.region[v.city_id];
                                if (v.county_id > 0)
                                    shtml += data.region[v.county_id];
                                shtml += "&nbsp;&nbsp;&nbsp;&nbsp;" + v.address;
                                shtml += '</p><div class="line"></div><div class="opr-bar">';
                                if (v.is_default)
                                    shtml += '<span class="moren moren-on"><i class="ico"></i>默认地址</span>';
                                else
                                    shtml += '<span class="moren" onclick="setUserAddress(' + v.id + ',this)"><i class="ico"></i>设为默认</span>';
                                shtml += '<a href="#"><i class="ico-qh"></i>切换</a><a href="edit_user_address?id=' + v.id + '"><i class="ico-bj"></i>编辑</a><a href="javascript:;" onclick="delUserAddress(' + v.id + ',this)"><i class="ico-sc"></i>删除</a></div></li>';
                            });

                            if (shtml != "") {
                                $("#thelist").append(shtml);
                                me.resetload();
                                is_show = (p - 1) * pagesize + ii;
                                console.log("已加载数据条数：" + is_show + " 一共有：" + data.count);
                                if (is_show == data.count) {
                                    me.lock();
                                    me.noData();
                                    is_over = true;
                                    $('.dropload-down > .dropload-refresh').html("无更多数据");
                                    $('.dropload-down > .dropload-load').html("无更多数据");
                                }
                            }


                        },
                        error: function (xhr, type) {
                            console.log('Ajax error!');
                            // 即使加载出错，也得重置
                            is_over = true;
                            me.lock();
                            return false;
                        }
                    });
                } else {
                    me.lock();
                    me.noData();
                    $('.dropload-down > .dropload-refresh').html("");
                }

            }
        });
        console.log(" toatal" + now_total);
        if (now_total == 0) {
            dropload.noData();
            $('.dropload-down').remove();
        } else if (now_total <= pagesize) {
            console.log("<" + pagesize);
            dropload.lock();
            $('.dropload-down > .dropload-load').html("");
            $('.dropload-down > .dropload-refresh').html("");
        }

    });
</script>
<?php
if ($this->JSAPI) {
    ?>
    <script src="/style/js/jweixin-1.0.0.js"></script>
    <script>
        wx.config({
            appId: '<?=$this->JSAPI['appId'];?>',
            timestamp: <?=$this->JSAPI['timestamp'];?>,
            nonceStr: '<?=$this->JSAPI['nonceStr'];?>',
            signature: '<?=$this->JSAPI['signature'];?>',
            jsApiList: [
                // 所有要调用的 API 都要加到这个列表中
                'hideOptionMenu',
                'hideMenuItems',
                'hideAllNonBaseMenuItem'
            ]
        });
        wx.ready(function () {
            // 在这里调用 API
            wx.hideOptionMenu();
            //wx.hideAllNonBaseMenuItem();
        });
    </script>
    <?
}
?>
<!--底部导航-->
<div class="foot-menu">
    <ul class="clearfix">
        <li><a href="/index/index"><i class="ico-1"></i><span>首页</span></a></li>
        
        <li><a href="/cart/cart"><i class="ico-5"></i><span>购物车</span></a></li>
        <li><a href="#"><i class="ico-3"></i><span>客服</span></a></li>
        <li class="on"><a href="/user/index"><i class="ico-4"></i><span>我的</span></a></li>
    </ul>
</div>
</body>
</html>